package com.donggua.aichebao.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.ViewStub;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.donggua.aichebao.commonui.load.LoadInputLayout;
import com.donggua.aichebao.login.R;
import com.third.login.ThirdLoginAuthListener;
import com.third.login.ThirdLoginBase;
import com.third.login.ThirdLoginData;
import com.third.login.ThirdLoginPlatform;

/**
 * Created by fzh on 2017/8/4.
 */

public abstract class DGLoginCommonBase implements View.OnClickListener{
    protected static final int MESSAGELOGIN = 0;
    protected static final int PASSWORDLOGIN = 1;
    protected Activity act;
    protected View mainView;
    protected LoadInputLayout passwordLoadLayout;
    protected LoadInputLayout accoountLoadLayout;
    protected ViewStub stubPasswordLogin;
    protected Button btnLogin;
    protected int loginStyle = MESSAGELOGIN;
    protected DGThirdLogin dgThirdLogin;
    protected LoginThread loginThread;
    public final static int LOGIN = 1;
    public final static int QQLOGIN = 2;
    public final static int WEIXINLOGIN = 3;
    public final static int SINALOGIN = 4;
    public final static int ERROR = 100;
    protected Handler uiHandler;
    public DGLoginCommonBase(Activity act,View mainView){
        this.act = act;
        this.mainView = mainView;
        init();
        setUiHandler();
    }

    protected void setUiHandler(){
        uiHandler = new Handler(Looper.getMainLooper()){
          public void handleMessage(Message msg){
              super.handleMessage(msg);
              handleUiMessage(msg);
          }
        };
    }


    protected void handleUiMessage(Message msg){

    }
    public String getUserName(){

        return accoountLoadLayout.getInputText();
    }

    public String getPassword(){

        return passwordLoadLayout.getInputText();
    }

    public void onStart(){
        if(loginThread == null){
            loginThread = new LoginThread();
            loginThread.startThread();
        }
    }

    public void onStop(){
        if(loginThread != null){
            loginThread.quitThread();
            loginThread = null;
        }
    }

    protected  void init(){
        stubPasswordLogin = (ViewStub)mainView.findViewById(R.id.stubPasswordLogin);
        btnLogin  = (Button)mainView.findViewById(R.id.btnCommonLogin);
        btnLogin.setOnClickListener(this);
    }

    public void doOnClick(View view){

    }

    protected void handleLoging(int msg){
        if(loginThread != null){
            loginThread.handleMessage(msg);
        }
    }
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btnCommonLogin){
            //act.finish();
            handleLoging(LOGIN);
        }else{
            doOnClick(view);
        }
    }


    public abstract void onLogin();

    public void onDestroy(){
        if(dgThirdLogin != null){
            dgThirdLogin.onDestroy();
        }

    }

    public  void onActivityResult(int requestCode, int resultCode, Intent data){
        if(dgThirdLogin != null){
            dgThirdLogin.onActivityResult(requestCode,resultCode,data);
        }
    }

    public  void onSaveInstanceState(Bundle outState){
        if(dgThirdLogin != null){
            dgThirdLogin.onSaveInstanceState(outState);
        }
    }

    private class LoginThread extends Thread{
        private Handler handler;

        public void run(){
            super.run();
            Looper.prepare();
            handler = new Handler(){
              public void handleMessage(Message msg){
                  super.handleMessage(msg);
                  switch (msg.what){
                      case LOGIN:
                          Log.e("login"," onLogin();");
                          onLogin();
                          break;
                      case QQLOGIN:
                          Log.e("login"," QQLOGIN");
                          dgThirdLogin.qqLogin();
                          break;
                      case WEIXINLOGIN:
                          dgThirdLogin.weiXinLogin();
                          break;
                      case SINALOGIN:
                          dgThirdLogin.sinaLogin();
                          break;
                  }
              }
            };
            synchronized (this){
                this.notify();
            }
            Looper.loop();

        }

        private void checkHandler(){
            if(handler == null){
                synchronized (this){
                    try {
                        this.wait(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public void startThread(){
            this.start();
        }
        public void quitThread(){
            checkHandler();
            if(handler != null){
                handler.getLooper().quit();
            }
        }

        public void handleMessage(int msg){
            checkHandler();
            if(handler != null){
                handler.sendEmptyMessage(msg);
            }
        }
    }

    protected void handleErrorMsg(int error,String msg){
        Message message = uiHandler.obtainMessage();
        message.what = ERROR;
        message.obj = message;
        message.arg1 = error;
        uiHandler.sendMessage(message);

    }
}
